package com.wus.gateway.config.handler;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.SaTokenException;
import com.wus.common.core.constant.AccountConstants;
import com.wus.common.core.domain.base.Result;
import com.wus.common.core.exception.BusinessException;
import com.wus.gateway.exception.NotAuthException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.bind.support.WebExchangeBindException;

/**
 * @Description: 网关异常统一处理
 * @Author: wus
 * @Date: 2024/12/17 20:41
 */
@RestControllerAdvice
public class GatewayExceptionHandler {

    private final Logger logger = LoggerFactory.getLogger(GatewayExceptionHandler.class);

    /**
     * 业务异常处理
     * @param ex
     * @return
     */
    @ExceptionHandler(value = BusinessException.class)
    public Result<String> handleBusinessException(BusinessException ex) {
        this.printExceptionMsg(ex);
        return Result.fail(ex.getMessage());
    }

    /**
     * 400 - Bad Request
     * 参数绑定校验异常捕捉处理
     */
    @ExceptionHandler(WebExchangeBindException.class)
    public Result<String> handleWebExchangeBindException(WebExchangeBindException ex) {
        this.printExceptionMsg(ex);
        BindingResult bindingResult = ex.getBindingResult();
        StringBuilder msgSb = new StringBuilder();
        for (FieldError error : bindingResult.getFieldErrors()) {
            if (!msgSb.isEmpty()) {
                msgSb.append(",");
            }
            msgSb.append(error.getDefaultMessage());
            break;
        }
        return Result.fail(HttpStatus.BAD_REQUEST.value(), msgSb.toString());
    }


    /**
     * 未认证异常捕捉处理
     * @param ex
     * @return
     */
    @ExceptionHandler(value = NotAuthException.class)
    public Result<String> handleNotAuthException(Exception ex) {
        this.printExceptionMsg(ex);
        return Result.fail(AccountConstants.ACCOUNT_NOT_LOGIN_CODE, ex.getMessage());
    }

    /**
     * 未认证异常捕捉处理
     * @param ex
     * @return
     */
    @ExceptionHandler(value = NotLoginException.class)
    public Result<String> handleNotLoginException(Exception ex) {
        this.printExceptionMsg(ex);
        return Result.fail(AccountConstants.ACCOUNT_NOT_LOGIN_CODE, ex.getMessage());
    }

    /**
     * 未认证异常捕捉处理
     * @param ex
     * @return
     */
    @ExceptionHandler(value = SaTokenException.class)
    public Result<String> handleSaTokenException(Exception ex) {
        this.printExceptionMsg(ex);
        return Result.fail(AccountConstants.ACCOUNT_NOT_LOGIN_CODE, ex.getMessage());
    }

    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public Result<String> handleOtherException(Exception ex) {
        this.printExceptionMsg(ex);
        return Result.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage());
    }

    private void printExceptionMsg(Exception ex) {
        logger.error("异常信息: ", ex);
    }
}
